package org.adl.registry.website.mail;

import java.util.ArrayList;
import java.util.Iterator;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.servlet.ServletRequest;

/**
 *
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class EmailMessage
{
    /**
     *
     */
    private InternetAddress mSender;
    
    /**
     *
     */
    private ArrayList<InternetAddress> mRecipients;
    
    /**
     *
     */
    private String mSubject;
    
    /**
     *
     */
    private StringBuffer mMessageBody;
    
    /** Creates a new instance of EmailMessage */
    public EmailMessage()
    {
        this.mRecipients = new ArrayList<InternetAddress>();
        this.mMessageBody = new StringBuffer();
    }
    
    /**
     * Gets the message sender.
     * @return
     */
    public InternetAddress getSender()
    {
        return this.mSender;
    }
    
    /**
     * Sets the message sender.
     * @param iSender
     */
    public void setSender(String iSender)
    throws AddressException
    {
        InternetAddress sender = new InternetAddress(iSender);
        
        this.setSender(sender);
    }
    
    /**
     * Sets the message sender.
     * @param iSender
     */
    public void setSender(InternetAddress iSender)
    {
        this.mSender = iSender;
    }
    
    /**
     * Gets the list of message recipients.
     * @return
     */
    public ArrayList<InternetAddress> getRecipients()
    {
        return this.mRecipients;
    }
    
    public void addRecipient(String iRecipient)
    throws AddressException
    {
        InternetAddress recipient = new InternetAddress(iRecipient);
        
        this.addRecipient(recipient);
    }
    
    public void addRecipient(InternetAddress iRecipient)
    {
        this.mRecipients.add(iRecipient);
    }
    
    /**
     * Gets the message subject.
     * @return
     */
    public String getSubject()
    {
        return this.mSubject;
    }
    
    /**
     * Sets the message subject.
     *
     * @param iSubject
     */
    public void setSubject(String iSubject)
    {
        this.mSubject = iSubject;
    }
    
    /**
     * Gets the message body.
     * @return
     */
    public String getMessageBody()
    {
        return this.mMessageBody.toString();
    }
    
    public void addToBody(String value)
    {
        this.mMessageBody.append(value);
    }
    
    public void addLineToBody(String value)
    {
        this.addToBody(value);
        this.addToBody("\r");
    }

    /**
     * Sends the email message.
     * @throws javax.mail.MessagingException
     */
    public void send()
    throws MessagingException
    {
        InternetAddress[] recipients = new InternetAddress[this.mRecipients.size()];
        
        int iteration = 0;
        
        Iterator<InternetAddress> iterator = this.mRecipients.iterator();
        
        while(iterator.hasNext())
        {
            recipients[iteration] = iterator.next();
            
            iteration++;
        }
        
        if (this.mRecipients.size() == 0)
        {
            String recipient = MailUtil.getSupportEmailAddress();
            this.addRecipient(recipient);
        }
        
        MailUtil.sendEmail(this.mSender, recipients, this.mSubject, this.getMessageBody());
    }
    
    /**
     * Sends the email message.
     * @param iRequest
     * @throws javax.mail.MessagingException
     */
    public void send(ServletRequest iRequest)
    throws MessagingException
    {
        String hostInfo = MailUtil.getHostInfoFromRequestAsString(iRequest);
        
        this.mMessageBody.append(hostInfo);
        
        this.send();
    }
}
